﻿using LD.Host;
using LD.Tool;
using PubSub;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LD.Tool
{
    [Tool(Text = "基恩士机种切换", FrmParam = "ToolPartNoChangeFrm", ToolType = ToolEnum.项目)]
    public class ToolPartNoChange : BaseTool
    {
        public override OpResult Execute(TB_Tool model)
        {
            try
            {
                stopwatch.Start();
                if (model.Param != null)
                {
                    int id = Appbus.PartNo.Config.Id;
                    var param = Newtonsoft.Json.JsonConvert.DeserializeObject<ToolTcpIpParam>(model.Param);
                    if (param.SplitChart)
                        param.Cmd = param.Cmd + id + "\r";
                    string res = CommunicateFactory.Instance.TcpIp[param.TcpIpName].Send(param.Cmd, param.TimeOut);

                    if (param.IsThanResult)
                    {
                        if (param.ThanResult.Equals(res))
                            isSuccess = true;
                    }
                    else
                        isSuccess = true;
                }
                if (!isSuccess)
                    Log(model, $"基恩士机种切换失败", LogLevel.警告);
            }
            catch (Exception ex)
            {
                Log(model, ex.Message, LogLevel.严重);
            }
            finally
            {
                stopwatch.Stop();
            }
            return new OpResult() { IsSuccess = isSuccess, Message = message, ExecuteTime = stopwatch.ElapsedMilliseconds };
        }
    }
}
